package org.example.struct.array2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author liyishan
 * @date 2024/7/18 09:49
 * @apiNote
 */

public class Test03112MinimumTime {

    public int[] minimumTime(int n, int[][] edges, int[] disappear) {
        List<int[]>[] adj = new List[n];
        for(int i = 0;i<n;i++){
            adj[i] = new ArrayList<>();
        }
        for(int[] edge : edges){
            int u = edge[0],v = edge[1],length = edge[2];
            adj[u].add(new int[]{v,length});
            adj[v].add(new int[]{u,length});
        }
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
        pq.offer(new int[]{0,0});
        int[] answer = new int[n];
        Arrays.fill(answer,-1);
        answer[0] = 0;
        while(!pq.isEmpty()){
            int[] arr = pq.poll();
            int t = arr[0],u = arr[1];
            if(t!= answer[u]){
                continue;
            }
            for(int[] next : adj[u]){
                int v = next[0],length = next[1];
                if(t + length < disappear[v] && (answer[v] == -1 || t+length < answer[v])){
                    pq.offer(new int[]{t+length,v});
                    answer[v] = t + length;
                }
            }
        }
        return answer;
    }
}
